Selecting Multiple Files

by Stefan Pendl

Home

Assist Tools Add-On

Assist Review

Passing DATA

Tip Corner

API Corner

Professional Software

Editing Keywords

Select Multiple Files

Eddie

Submissions

Index


'   Selecting multiple files, demo by Stefan Pendl, Dec 2005

'---code start
    Dir$ = "C:\"
    Filter$ = "Textfiles|*.txt;*.bat|All Files|*.*"
    Title$ = "Select Files"

    print FileExplorer$(Title$, Dir$, Filter$)
end

function FileExplorer$(Caption$, InitialDir$, FilterString$)
' function to select multiple files
'
' Usage:
'       FilterString in the form of {description}|{list of extensions}
'           extensions are seperated with semicolons (;)
'           multiple pairs are supported
'           field seperator is the pipe (|)
'
' the returned string consits of the number of files
' and the directory followed by the filenames
' all fields are seperated by semicolons (;)

    OFN.EXPLORER = hexdec("80000")
    z$ = chr$(0)

    struct ofn, _
        lStructSize        as ulong, _
        hwndOwner          as ulong, _
        hInstance          as ulong, _
        lpstrFilter$       as ptr, _
        lpstrCustomFilter$ as ptr, _
        nMaxCustFilter     as ulong, _
        nFilterIndex       as ulong, _
        lpstrFile$         as ptr, _
        nMaxFile           as ulong, _
        lpstrFileTitle$    as ptr, _
        nMaxFileTitle      as ulong, _
        lpstrInitialDir$   as ptr, _
        lpstrTitle$        as ptr, _
        flags              as ulong, _
        nFileOffset        as word, _
        nFileExtension     as word, _
        lpstrDefExt$       as ptr, _
        lCustData          as ulong, _
        lpfnHook           as ulong, _
        lpTemplateName$    as ptr

    Filter$ = ReplaceString$(FilterString$, "|", z$, "ALL", 0) + z$ + z$

    ofn.lStructSize.struct = len(ofn.struct)
    ofn.lpstrFilter$.struct = Filter$
    ofn.lpstrFile$.struct = z$ + space$(3072) + z$
    ofn.nMaxFile.struct = 3072
    ofn.lpstrFileTitle$.struct = space$(255) + z$
    ofn.nMaxFileTitle.struct = 255
    ofn.lpstrInitialDir$.struct = InitialDir$ + z$
    ofn.lpstrTitle$.struct = Caption$ + z$
    ofn.flags.struct = _OFN_PATHMUSTEXIST or _OFN_FILEMUSTEXIST or _OFN_ALLOWMULTISELECT or OFN.EXPLORER

    calldll #comdlg32, "GetOpenFileNameA",_
        ofn as struct,_
        ok  as boolean

    pointer = ofn.lpstrFile$.struct
    count = 0

    If ok Then
        FileExplorer$ = WinString(pointer)
        pointer = pointer + Len(FileExplorer$) + 1
        program$ = WinString(pointer)

        if program$ = "" then
            FileExplorer$ = DirName$(FileExplorer$); ";";BaseName$(FileExplorer$, "", "")
            count = 1
        else
            While program$ <> ""
                FileExplorer$ = FileExplorer$; ";"; program$
                pointer = pointer + Len(program$) + 1
                count = count + 1
                program$ = WinString(pointer)
            Wend
        end if
    End If

    FileExplorer$ = count; ";"; FileExplorer$
end function

function BaseName$(input$, ext$, sep$)
' returns the name of the given file
' if ext$ matches the last part of
' input$ it will be cut off
    if sep$ = "" then sep$ = "\"

    if len(ext$) > 0 and right$(input$, len(ext$)) = ext$ then _
        input$ = left$(input$,len(input$) - len(ext$))

    BaseName$ = input$
    bsPos = len(input$)
    while mid$(input$, bsPos, 1) <> sep$ and bsPos > 0
        bsPos = bsPos - 1
    wend

    if bsPos <> 0 then BaseName$ = right$(input$,len(input$) - bsPos)
end function

function DirName$(input$)
' returns the path of the given file

    DirName$ = input$
    bsPos = len(input$)
    while mid$(input$, bsPos, 1) <> "\" and bsPos > 0
        bsPos = bsPos - 1
    wend

    if bsPos <> 0 then DirName$ = left$(input$,bsPos - 1)
end function

function ReplaceString$(in$,ere$,repl$,occ$,pos)
' Function to replace expressions in strings
'
' Usage:
'
'
' ReplaceString$({string},{expression},{replacement},{occurrances},{startposition})
'
'       string .......... string to be parsed
'       expression ...... string to be replaced
'       replacement ..... substitute for expression
'       occurrances ..... ALL -> replaces all occurrances
'                         else the specified number
'       startposition ... position from where to start substitution
'
'   returns altered string
    ReplaceString$ = in$
    if upper$(occ$) = "ALL" then
        number = len(in$)
    else
        number = val(occ$)
    end if
    if pos < 0 then pos = 0

    while instr(ReplaceString$,ere$,pos) > 0
        pos = instr(ReplaceString$,ere$,pos)

        ReplaceString$ = left$(ReplaceString$,pos - 1) + repl$ + mid$(ReplaceString$,pos + len(ere$))

        if upper$(occ$) = "ALL" and number > 1 then
            pos = pos + len(repl$)
            number = number - 1
        else
            pos = len(ReplaceString$)
        end if
    wend
end function
'--- code end


Home

Assist Tools Add-On

Assist Review

Passing DATA

Tip Corner

API Corner

Professional Software

Editing Keywords

Select Multiple Files

Eddie

Submissions

Index